home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / term-source.lha / Colour.c < prev    next >
C/C++ Source or Header  |  1995-09-26  |  5KB  |  248 lines

  1. /*
  2. **    Colour.c
  3. **
  4. **    Colour table support code
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* ColourTablex12(const ColourTable *Table,UWORD *Dest12,WORD NumColours):
  15.      *
  16.      *    Convert colour table into 12 bit precision colours.
  17.      */
  18.  
  19. STATIC VOID __regargs
  20. ColourTablex12(const ColourTable *Table,UWORD *Dest12,WORD NumColours)
  21. {
  22.     ULONG    r,g,b;
  23.     WORD    i;
  24.  
  25.     for(i = 0 ; i < NumColours ; i++)
  26.     {
  27.         r = Table -> Entry[i] . Red        >> 28;
  28.         g = Table -> Entry[i] . Green    >> 28;
  29.         b = Table -> Entry[i] . Blue    >> 28;
  30.  
  31.         *Dest12++ = (r << 8) | (g << 4) | b;
  32.     }
  33. }
  34.  
  35.     /* Colour96xColourTable(const ULONG *Source96,ColourTable *Table,WORD NumColours):
  36.      *
  37.      *    Convert 96 bit precision colours for colour table.
  38.      */
  39.  
  40. VOID __regargs
  41. Colour96xColourTable(const ULONG *Source96,ColourTable *Table,WORD NumColours)
  42. {
  43.     WORD i;
  44.  
  45.     for(i = 0 ; i < NumColours ; i++)
  46.     {
  47.         Table -> Entry[i] . Red        = *Source96++;
  48.         Table -> Entry[i] . Green    = *Source96++;
  49.         Table -> Entry[i] . Blue    = *Source96++;
  50.     }
  51. }
  52.  
  53.     /* Colour12xColourTable(const UWORD *Source12,ColourTable *Table,WORD NumColours):
  54.      *
  55.      *    Convert 12 bit precision colours for colour table.
  56.      */
  57.  
  58. VOID __regargs
  59. Colour12xColourTable(const UWORD *Source12,ColourTable *Table,WORD NumColours)
  60. {
  61.     Colour12x96(Source12,(ULONG *)Table -> Entry,NumColours);
  62. }
  63.  
  64.     /* Colour12x96(const UWORD *Source12,ULONG *Dest96,WORD NumColours):
  65.      *
  66.      *    Convert 12 bit precision colours into 96 bit colours.
  67.      */
  68.  
  69. VOID __regargs
  70. Colour12x96(const UWORD *Source12,ULONG *Dest96,WORD NumColours)
  71. {
  72.     ULONG r,g,b;
  73.     UWORD v;
  74.  
  75.     do
  76.     {
  77.         v = *Source12++;
  78.  
  79.         r = (v & 0xF00) >> 8;
  80.         g = (v & 0x0F0) >> 4;
  81.         b = (v & 0x00F);
  82.  
  83.         *Dest96++ = SPREAD((r << 4) | r);
  84.         *Dest96++ = SPREAD((g << 4) | g);
  85.         *Dest96++ = SPREAD((b << 4) | b);
  86.     }
  87.     while(--NumColours);
  88. }
  89.  
  90.     /* Colour96x12(const UWORD *Source12,ULONG *Dest96,WORD NumColours):
  91.      *
  92.      *    Convert 96 bit precision colours into 12 bit colours.
  93.      */
  94.  
  95. VOID __regargs
  96. Colour96x12(const ULONG *Source96,UWORD *Dest12,WORD NumColours)
  97. {
  98.     ULONG r,g,b;
  99.  
  100.     do
  101.     {
  102.         r = (*Source96++) >> 28;
  103.         g = (*Source96++) >> 28;
  104.         b = (*Source96++) >> 28;
  105.  
  106.         *Dest12++ = (r << 8) | (g << 4) | b;
  107.     }
  108.     while(--NumColours);
  109. }
  110.  
  111.     /* CopyColourEntry(const ColourTable *Source,ColourTable *Destination,WORD From,WORD To):
  112.      *
  113.      *    Copy one colour register from one place to another.
  114.      */
  115.  
  116. VOID __regargs
  117. CopyColourEntry(const ColourTable *Source,ColourTable *Destination,WORD From,WORD To)
  118. {
  119.     if(From >= 0 && To >= 0 && From < Source -> NumColours && To < Destination -> NumColours)
  120.     {
  121.         Destination -> Entry[To] . Red        = Source -> Entry[From] . Red;
  122.         Destination -> Entry[To] . Green    = Source -> Entry[From] . Green;
  123.         Destination -> Entry[To] . Blue        = Source -> Entry[From] . Blue;
  124.     }
  125. }
  126.  
  127.     /* ColourTablex96(const ColourTable *Table,ULONG *Dest96):
  128.      *
  129.      *    Convert colour table to 96 bit RGB palette.
  130.      */
  131.  
  132. VOID __regargs
  133. ColourTablex96(const ColourTable *Table,ULONG *Dest96)
  134. {
  135.     WORD i;
  136.  
  137.     for(i = 0 ; i < Table -> NumColours ; i++)
  138.     {
  139.         *Dest96++ = Table -> Entry[i] . Red;
  140.         *Dest96++ = Table -> Entry[i] . Green;
  141.         *Dest96++ = Table -> Entry[i] . Blue;
  142.     }
  143. }
  144.  
  145.     /* DeleteColourTable(ColourTable *Table):
  146.      *
  147.      *    Delete a colour table.
  148.      */
  149.  
  150. VOID __regargs
  151. DeleteColourTable(ColourTable *Table)
  152. {
  153.     FreeVecPooled(Table);
  154. }
  155.  
  156.     /* CreateColourTable(WORD NumEntries,UWORD *Source12,ULONG *Source96):
  157.      *
  158.      *    Create a colour table and fill it with given data.
  159.      */
  160.  
  161. ColourTable * __regargs
  162. CreateColourTable(WORD NumEntries,const UWORD *Source12,const ULONG *Source96)
  163. {
  164.     ColourTable *Record;
  165.  
  166.     if(Record = (ColourTable *)AllocVecPooled(sizeof(ColourTable) + NumEntries * sizeof(ColourEntry),MEMF_ANY | MEMF_CLEAR))
  167.     {
  168.         WORD i;
  169.  
  170.         Record -> NumColours = NumEntries;
  171.  
  172.         for(i = 0 ; i < NumEntries ; i++)
  173.         {
  174.             Record -> Entry[i] . One    = 1;
  175.             Record -> Entry[i] . Which    = i;
  176.         }
  177.  
  178.         Record -> Entry[NumEntries] . One = 0;
  179.  
  180.         if(Source12)
  181.             Colour12x96(Source12,(ULONG *)Record -> Entry,NumEntries);
  182.  
  183.         if(Source96)
  184.         {
  185.             for(i = 0 ; i < NumEntries ; i++)
  186.             {
  187.                 Record -> Entry[i] . Red    = *Source96++;
  188.                 Record -> Entry[i] . Green    = *Source96++;
  189.                 Record -> Entry[i] . Blue    = *Source96++;
  190.             }
  191.         }
  192.     }
  193.  
  194.     return(Record);
  195. }
  196.  
  197.     /* LoadColours(struct ViewPort *VPort,const ColourTable *Table,const UWORD *Colour12,WORD NumColours):
  198.      *
  199.      *    Load a colour table.
  200.      */
  201.  
  202. VOID __regargs
  203. LoadColourTable(struct ViewPort *VPort,const ColourTable *Table,const UWORD *Colour12,WORD NumColours)
  204. {
  205.     if(GfxBase -> LibNode . lib_Version >= 39 && Table)
  206.         LoadRGB32(VPort,(ULONG *)Table -> Entry);
  207.     else
  208.     {
  209.         if(Colour12)
  210.             LoadRGB4(VPort,(UWORD *)Colour12,NumColours);
  211.     }
  212. }
  213.  
  214.     /* SetColour12(struct ViewPort *VPort,WORD Number,UWORD r,UWORD g,UWORD b):
  215.      *
  216.      *    Load a single colour entry in 12 bit colour precision.
  217.      */
  218.  
  219. VOID __regargs
  220. SetColour12(struct ViewPort *VPort,WORD Number,UWORD r,UWORD g,UWORD b)
  221. {
  222.     SetRGB4(VPort,Number,r,g,b);
  223. }
  224.  
  225.     /* SetColour96(struct ViewPort *VPort,ULONG Number,ULONG r,ULONG g,ULONG b):
  226.      *
  227.      *    Load a single colour entry in 96 bit colour precision.
  228.      */
  229.  
  230. VOID __regargs
  231. SetColour96(struct ViewPort *VPort,ULONG Number,ULONG r,ULONG g,ULONG b)
  232. {
  233.     if(GfxBase -> LibNode . lib_Version >= 39)
  234.     {
  235.         ULONG Table[5];
  236.  
  237.         Table[0] = (1L << 16) | Number;
  238.         Table[1] = r;
  239.         Table[2] = g;
  240.         Table[3] = b;
  241.         Table[4] = 0;
  242.  
  243.         LoadRGB32(VPort,Table);
  244.     }
  245.     else
  246.         SetRGB4(VPort,Number,r >> 28,g >> 28,b >> 28);
  247. }
  248.